//
//  EditAccountViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/7.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "EditAccountViewController.h"
#import "AccountViewController.h"

@interface EditAccountViewController ()


@property (nonatomic,strong)UITextField * name_textField;
@property (nonatomic,strong)UITextField * depositbank_textField;
@property (nonatomic,strong)UITextField * smalldepositbank_textField;
@property (nonatomic,strong)UITextField * cardNumber_textField;
@property (nonatomic,strong)UITextField * smallcardNumber_textField;
@property (nonatomic,strong)UITextField * password_textField;
@property (nonatomic,strong)UIScrollView * SC;

@end

@implementation EditAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    [self createUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"编辑收款账户";
    
}

-(void)createUI{
    
    [self.rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    
    _SC=[[UIScrollView alloc]initWithFrame:CGRectMake(0, kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight)];
    [self.view addSubview:_SC];
    CGFloat H = G_SCREENHEIGHT;
    if (G_SCREENHEIGHT<(20+6*90+400)) {
        H =20+6*90+400;
    }
    
    _SC.contentSize = CGSizeMake(G_SCREENWIDTH, H );
    
    
    
    _name_textField = [[UITextField alloc]init];
    [self setText:@"姓名" andTF:_name_textField andIndex:0 andPlaceholder:@"请输入姓名" andKeyboardType:UIKeyboardTypeDefault];
    _depositbank_textField = [[UITextField alloc]init];
    [self setText:@"开户银行" andTF:_depositbank_textField andIndex:1 andPlaceholder:@"请输入开户银行" andKeyboardType:UIKeyboardTypeDefault];
    _smalldepositbank_textField = [[UITextField alloc]init];
    [self setText:@"开户支行" andTF:_smalldepositbank_textField andIndex:2 andPlaceholder:@"请输入开户支行" andKeyboardType:UIKeyboardTypeDefault];
    _cardNumber_textField = [[UITextField alloc]init];
    [self setText:@"银行卡号" andTF:_cardNumber_textField andIndex:3 andPlaceholder:@"请输入银行卡号" andKeyboardType:UIKeyboardTypeNumberPad];
    
    
    _smallcardNumber_textField = [[UITextField alloc]init];
    [self setText:@"确认卡号" andTF:_smallcardNumber_textField andIndex:4 andPlaceholder:@"请再次输入银行卡号" andKeyboardType:UIKeyboardTypeNumberPad];
    
    
    _password_textField = [[UITextField alloc]init];
    [self setText:@"资金密码" andTF:_password_textField andIndex:5 andPlaceholder:@"请输入资金密码" andKeyboardType:UIKeyboardTypeDefault];
    _password_textField.secureTextEntry=YES;
    
    NSDictionary * account = _dataSource.account;

    //{
    //    branch = "\U897f\U5b89";
    //    "card_number" = 6222600810020865911;
    //    "create_address" = "\U4e2d\U56fd\U94f6\U884c";
    //    name = "\U738b\U51ef";
    //}
    
    _name_textField.text=[NSString stringWithFormat:@"%@",account[@"name"]];
    _depositbank_textField.text=[NSString stringWithFormat:@"%@",account[@"create_address"]];
    _smalldepositbank_textField.text=[NSString stringWithFormat:@"%@",account[@"branch"]];
    _cardNumber_textField.text=[NSString stringWithFormat:@"%@",account[@"card_number"]];
    _smallcardNumber_textField.text=[NSString stringWithFormat:@"%@",account[@"card_number"]];
    

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [self.view  addGestureRecognizer:singleTapGesture];
    
}

#pragma mark - gesture actions
- (void)closeKeyboard:(UITapGestureRecognizer *)recognizer {
    [self.view endEditing:YES];
}



-(void)setText:(NSString *)text andTF:(UITextField *)textField andIndex:(NSInteger)index andPlaceholder:(NSString *)placeholder andKeyboardType:(UIKeyboardType)Type{
    CGFloat T = 20 + index*90;
    
    UILabel * lab = [[UILabel alloc]init];
    [self.SC addSubview:lab];
    [self setLab:lab withColor:RGB(30, 63, 88) andFont:12 andText:text andTextAlignment:NSTextAlignmentLeft];
    lab.frame=CGRectMake(15, T, 200, 30);
    
    
    [self.SC addSubview:textField];
    [self setTextField:textField WithPlaceholder:placeholder and:Type];
    textField.frame=CGRectMake(15, T+20, G_SCREENWIDTH-30, 40);
    
    UIView * line = [[UIView alloc]init];
    line.backgroundColor=RGB(231, 235, 238);
    [self.SC addSubview:line];
    line.frame=CGRectMake(15, T+20+39, G_SCREENWIDTH-30, 1);
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    //    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}


-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
}

/*
修改用户收款方式

1、接口地址

请求方式：post
请求接口：otcpayment/updatepayment/
是否登录：是
 
 参数名    参数类型    含义    备注
 payment_id    整型    收款方式id
 paytype_id    整型    支付方式id
 create_address    字符串    开户行
 branch    字符串    开户支行
 card_number    字符串    银行卡号
 asset_password    字符串    用户资产密码    lenth >= 6
  */
#pragma mark-更新收款账户
-(void)rightAction{
    
    NSString * URL = [NSString stringWithFormat:@"%@otcpayment/updatepayment/",DEFAULTSEVERBASEURL];
    
    WS(weakSelf);
    
    
    if (_password_textField.text.length<1) {
        [HUD showErrorMessage:@"请输入资金密码！"];
        return;
    }
    
    if (_depositbank_textField.text.length<1) {
        [HUD showErrorMessage:@"请输入开户行！"];
        return;
    }
    
    if (_cardNumber_textField.text.length<1) {
        [HUD showErrorMessage:@"请输入卡号！"];
        return;
    }
    
    if (_name_textField.text.length<1) {
        [HUD showErrorMessage:@"请输入姓名！"];
        return;
    }

    NSDictionary * Dic =@{
                          @"payment_id":_dataSource.ID,
                          @"paytype_id":_dataSource.paytype_id,
                          @"create_address":_depositbank_textField.text,
                          @"branch":_smalldepositbank_textField.text,
                          @"card_number":_cardNumber_textField.text,
                          @"asset_password":_password_textField.text,
                          @"name":_name_textField.text,
                          @"access_token":[LoginModel GetValue:DL_token]
                          };
    
    
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        if ([code isEqualToString:@"0"]) {
            
            [HUD showSuccessMessage:msg];
            
            [self performSelector:@selector(delayMent) withObject:nil afterDelay:2.0];

            
            
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
        [HUD dismissInView:weakSelf.bgView];
        
    }];
}
-(void)delayMent{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[AccountViewController class]]) {
            
            AccountViewController *A =(AccountViewController *)controller;
            [self.navigationController popToViewController:A animated:YES];
            
        }
    }
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

